<?php

namespace App\Http\Controllers;

use DB;
use Illuminate\Support\Facades\Redis;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
//        $this->middleware('auth');
    }

    public function index()
    {
        $this->chatList();
    }

    //聊天室列表
    public function chatList()
    {
        //房间列表
        //$room_list = ();
        $room_type = DB::select('select * from room_type');
        //
        foreach($room_type as $type)
        {
            $room_list[$type->type_id] = DB::select('select * from room where type_id = ?',[ $type->type_id ]);
        }

        return view('chat.chatList', ['room_type' => $room_type,'room_list'=>$room_list]);
    }

    //聊天室
    public function chatRoom(Request $request,$id)
    {
        $user_info = Auth::user();
        $room_info = DB::table('room')->where('id', $id )->first();
        //是否存入redis
        if (!Redis::hExists('user',$user_info->id))
        {
            $user['id'] = $user_info->id;
            $user['name'] = $user_info->name;
            Redis::hSet('user',$user_info->id,json_encode($user));
        }

        return view('chat.chatRoom', ['room_info' => $room_info,'user_info'=>$user_info]);
    }

    //直播间列表
    public function liveList()
    {
        $room_type = DB::select('select * from live_room_type');
        //查询开播的会员
        foreach($room_type as $type)
        {
            $room_list[$type->type_id] = DB::table('live_room')->where('is_play',1)->where('type_id',$type->type_id)->get();
        }
        return view('live.liveList', ['room_type' => $room_type,'room_list' => $room_list]);
    }
    //直播间 Live
    public function liveRoom(Request $request,$id)
    {
        $user_info = Auth::user();
        $room_info = DB::table('live_room')->where('id', $id )->first();
        //是否存入redis
        if (!Redis::hExists('user',$user_info->id))
        {
            $user['id'] = $user_info->id;
            $user['name'] = $user_info->name;
            Redis::hSet('user',$user_info->id,json_encode($user));
        }
        return view('live.liveRoom', ['room_info' => $room_info,'user_info'=>$user_info]);
    }

    //房间
    public function room( Request $request,$id)
    {

    }

    //关于我们
    public function about()
    {
        return view('article.about');
    }

    //视频直播使用帮助
    public function help()
    {
        return view('article.help');

    }

}
